package com.woniuxy.auth.mapper;

import com.hc.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

/**
 * @ClassName UserMapper
 * @Description
 * @Author shanshan
 * @Date 2022/10/23 8:02
 * @Version 1.0
 **/
public interface UserMapper {
    @Insert("insert into hc_user values(null, #{account},#{password},#{role.id},#{status})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insert(User user);

    @Select("select * from hc_user where account=#{account} and password=#{password}")
    User getByAccAndPwd(@Param("account") String account, @Param("password") String password);

    @Select("select u.*,r.name from hc_user u, hc_role r where u.role_id = r.id and account=#{account} and password=#{password}")
    @ResultMap(value="roleResultMap")
//    @Results(
//            {
//                    @Result(column = "id", property = "id", id= true),
//                    @Result(column = "role_id", property = "role.id")
//            }
//    )
    User getByAccountAndPwd(User user);

    @Delete("delete from rbac_user where id=#{id}")
    void delete(int id);


    @Select("select u.*,r.name from hc_user u, hc_role r where u.role_id = r.id and u.id=#{id}")
    @Results(id = "roleResultMap", value =
            {
                    @Result(column = "id", property = "id", id = true),
                    //多对一，懒加载
                    @Result(column = "role_id", property = "role", one=@One(select = "com.woniuxy.auth.mapper.RoleMapper.getById",fetchType = FetchType.LAZY))

            }
    )
    User getById(int id);


    //通过唯一的账户得到角色
    @Select("select u.*,r.name from hc_user u, hc_role r where u.role_id = r.id and u.account=#{account}")
    @Results(id = "roleResultMap2", value =
            {
                    @Result(column = "id", property = "id", id = true),
                    @Result(column = "role_id", property = "role", many=@Many(select = "com.woniuxy.auth.mapper.getById",fetchType = FetchType.LAZY))
            }
    )
    User getByAccount(String account);


    /**
     * 多条件修改 动态修改
     *
     * @param user
     */
    @UpdateProvider(type = UserMapper.SqlProvider.class, method = "update")
    void update(User user);

    public static class SqlProvider {
        public static String update(User user) {
            //账号不可变
            return new SQL() {
                {
                    UPDATE("hc_user");
                    if (user.getImage() != null && user.getImage() != "''") {
                        SET("image=#{image}");
                    }
                    if (user.getName() != null && user.getName() != "''") {
                        SET("name=#{name}");
                    }
                    if (user.getPassword() != null && user.getPassword() != "''") {
                        SET("password=#{password}");
                    }
                    if (user.getProfile() != null && user.getProfile() != "''") {
                        SET("profile=#{profile}");
                    }
                    if (user.getRole() != null && user.getRole().getId() != 0) {
                        SET("role_id=#{role.id}");
                    }
                    if (user.getStatus() != null && user.getStatus() != "''") {
                        SET("status=#{status}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
        }
    }
}
